import {deployedContractData} from "./constDefine";
import { Abi, AbiFunction } from "abitype";
import {GenericContract, InheritedFunctions} from "./contract"

let abiv = (deployedContractData.abi || []) as Abi;

console.log(abiv);

let abif = (abiv.filter(part => part.type === "function") as AbiFunction[]);
console.log(abif);
console.log("\n-------------------\n");

let abiquery = abif.filter(fn => {
    const isQueryableWithParams =
      (fn.stateMutability === "view" || fn.stateMutability === "pure") && fn.inputs.length > 0;
    return isQueryableWithParams;
  });

  console.log(abiquery);  
  console.log("\n-------------------\n");

  let abimap = abiquery.map(fn => {
    return {
      fn,
      inheritedFrom: ((deployedContractData as GenericContract)?.inheritedFunctions as InheritedFunctions)?.[fn.name],
    };
  });
  console.log(abimap);
  console.log("\n-------------------\n");
  let abisort = abimap.sort((a, b) => (b.inheritedFrom ? b.inheritedFrom.localeCompare(a.inheritedFrom) : 1));

  console.log(abimap);
  console.log("\n-------------------\n");
const functionsToDisplay = (
    ((deployedContractData.abi || []) as Abi).filter(part => part.type === "function") as AbiFunction[]
  )
    .filter(fn => {
      const isQueryableWithParams =
        (fn.stateMutability === "view" || fn.stateMutability === "pure") && fn.inputs.length > 0;
      return isQueryableWithParams;
    })
    .map(fn => {
      return {
        fn,
        inheritedFrom: ((deployedContractData as GenericContract)?.inheritedFunctions as InheritedFunctions)?.[fn.name],
      };
    })
    .sort((a, b) => (b.inheritedFrom ? b.inheritedFrom.localeCompare(a.inheritedFrom) : 1));

console.log(functionsToDisplay);    